package com.lsk.division;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.lsk.division.entity.*;
import com.lsk.division.mapper.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.PathVariable;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

@Service
public class MyService {
    @Autowired
    private AreaMapper areaMapper;
    @Autowired
    private CityMapper cityMapper;
    @Autowired
    private ProvinceMapper provinceMapper;
    @Autowired
    private StreetMapper streetMapper;
    @Autowired
    private VillageMapper villageMapper;


    public Result findAllProvince(){
        //Result result=new Result();
        //补全省份6位代码
        QueryWrapper<Province> queryWrapper=new QueryWrapper<>();
        queryWrapper.orderByAsc("code");
        try{
            List<Province> provinces = provinceMapper.selectList(queryWrapper);
            List<DataVo> dataVoList=new ArrayList<>();
            provinces.forEach(province -> {
                DataVo dataVo = new DataVo();
                dataVo.setLeaf(false);
                dataVo.setKey(province.getCode()+"0000");
                dataVo.setTitle(province.getName());
                dataVoList.add(dataVo);
            });
//            result.setSuccess(true);
//            result.setCode(200);
//            result.setData(dataVoList);
            return Result.SUCCESS(dataVoList);
        }
        catch (Exception e){
//            result.setSuccess(false);
//            result.setCode(500);
//            result.setData(e.getMessage());
            return Result.SUCCESS(e.getMessage());
        }

    }

    public Result findAllVillageByAreaCode(@PathVariable String code){

        if(code.length()!=6){
            return Result.ERROR("请输入正确的6位code");
        }else{
            try{
                QueryWrapper<Street> queryWrapper=new QueryWrapper<>();
                queryWrapper.orderByAsc("code");
                queryWrapper.eq("areaCode",code);
                List<Street> streetList = streetMapper.selectList(queryWrapper);
                List<DataVo> dataVoList=new ArrayList<>();
                streetList.forEach(street -> {
                    DataVo dataVo = new DataVo();
                    dataVo.setLeaf(false);
                    dataVo.setKey(street.getCode());
                    dataVo.setTitle(street.getName());
                    //根据乡镇编号查询
                    QueryWrapper<Village> qw=new QueryWrapper<>();
                    qw.orderByAsc("code");
                    qw.eq("streetCode",street.getCode());
                    List<Village> villageList = villageMapper.selectList(qw);
                    //封装乡村数据为dataVo
                    List<DataVo> dataVos=new ArrayList<>();
                    villageList.forEach(village -> {
                        DataVo vo = new DataVo();
                        vo.setKey(village.getCode());
                        vo.setTitle(village.getName());
                        dataVo.setLeaf(true);
                        dataVos.add(vo);
                    });
                    dataVo.setChildren(dataVos);
                    dataVoList.add(dataVo);
                });
                return Result.SUCCESS(dataVoList);
            }catch (Exception e){
                return Result.ERROR(e.getMessage());
            }
        }
    }

    public Result findAllChildren(@PathVariable String code){
        switch (code.length()){
            case 6:{
                //省、市、县区
                //全国
                if(code.equals("000000"))
                    return findAllProvince();
                    //2-6位为0则为省
                else if(code.substring(2).equals("0000")){
                    code=code.substring(0,2);
                    QueryWrapper<City> queryWrapperCity=new QueryWrapper<>();
                    queryWrapperCity.orderByAsc("code");
                    queryWrapperCity.eq("provinceCode",code);
                    List<City> cities = cityMapper.selectList(queryWrapperCity);
                    List<DataVo> cityData = new ArrayList<>();
                    cities.forEach(city -> {
                        cityData.add(new DataVo(city.getName(),city.getCode()+"00"));
                    });
                    return Result.SUCCESS(cityData);
                }
                //4-6为0则为地市
                else if(code.substring(4).equals("00")){
                    code=code.substring(0,4);
                    QueryWrapper<Area> queryWrapper=new QueryWrapper<>();
                    queryWrapper.orderByAsc("code");
                    queryWrapper.eq("cityCode",code);
                    List<Area> areas = areaMapper.selectList(queryWrapper);
                    List<DataVo> areaData = new ArrayList<>();
                    areas.forEach(area -> {
                        areaData.add(new DataVo(area.getName(),area.getCode()));
                    });
                    return Result.SUCCESS(areaData);


                }
                else{
                    // return findAllVillageByAreaCode(code);
                    //不应该递归查出，而应该只查出来乡镇
                    QueryWrapper<Street> queryWrapper = new QueryWrapper<>();
                    queryWrapper.orderByAsc("code");
                    queryWrapper.eq("areaCode",code);
                    List<Street> streets = streetMapper.selectList(queryWrapper);
                    List<DataVo> areaData = new ArrayList<>();
                    streets.forEach(street -> {
                        areaData.add(new DataVo(street.getName(),street.getCode()));
                    });
                    return Result.SUCCESS(areaData);


                }

            }
            case 9:{
                //街道
                //根据乡镇编号查询
                QueryWrapper<Village> qw=new QueryWrapper<>();
                qw.orderByAsc("code");
                qw.eq("streetCode",code);
                List<Village> villageList = villageMapper.selectList(qw);
                List<DataVo> villageVo = new ArrayList<>();
                villageList.forEach(village -> {
                    villageVo.add(new DataVo(village.getName(),village.getCode(),true));
                });
                return Result.SUCCESS(villageVo);
            }
            default:
                return Result.ERROR("输入的编码长度必须为2、4、6、9位");
        }
    }

    public Result findAllEasternNewArea(){
        //根据乡镇编号查询

        try {
            //qw.eq("streetCode",code);
            List<String> list = Arrays.asList(
                    "510185009", "510185010", "510185011", "510185012", "510185013", "510185014", "510185015",
                    "510185016", "510185017", "510185122", "510185126", "510185127", "510185128", "510185131", "510185132");
            List<DataVo> resultList=new ArrayList<>();
            list.forEach(item->{
                DataVo dataVo=new DataVo();
                List<DataVo> children = new ArrayList<>();
                //去街道表查出街道名称
                QueryWrapper<Street> streetQueryWrapper = new QueryWrapper<>();
                streetQueryWrapper.eq("code",item);
                Street street = streetMapper.selectOne(streetQueryWrapper);
                QueryWrapper<Village> qw = new QueryWrapper<>();
                qw.orderByAsc("code");
                //qw.in("streetCode",list);
                qw.eq("streetCode",item);
                //去乡村表根据街道编号查询
                System.out.println(street);
                List<Village> villageList = villageMapper.selectList(qw);
                villageList.forEach(village -> {
                    children.add(new DataVo(village.getName(),village.getCode(),true));
                });
                dataVo.setKey(street.getCode());
                dataVo.setTitle(street.getName());
                dataVo.setChildren(children);
                dataVo.setLeaf(false);
                resultList.add(dataVo);
            });
            return Result.SUCCESS(resultList);
        }catch (Exception e){
            return Result.ERROR(e.getMessage());
        }


    }
}
